package com.chenenjun.control;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chenenjun.service.IUserService;
import com.chenenjun.service.UserServiceImpl;
import com.chenenjun.vo.BBSUser;

/**
 * Servlet implementation class UserController
 */
@WebServlet(name="/UserController", urlPatterns={"/user"},
		initParams={@WebInitParam(name="", value="")})
public class UserController extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	private IUserService service = new UserServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		RequestDispatcher dispatcher = null;
		switch (action) {
		case "login":{
			BBSUser user = new BBSUser();
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			user.setUsername(username);
			user.setPassword(password);
			BBSUser userLogin = service.login(user);
			if(userLogin != null){
				request.getSession().setAttribute("user", userLogin);
				request.setAttribute("information", "登录成功！欢迎您"+userLogin.getUsername());
			} else {
				request.setAttribute("information", "非法登录！");
			}
			dispatcher = request.getRequestDispatcher("article?action=query&curpage=1");
			break;
		}
		case "pic":{
			String id = request.getParameter("id");
			byte [] buffer = service.queryPicById(Integer.parseInt(id));
			response.setContentType("image/jpeg");//设置响应的结果是jpeg格式的图片，但是这是浏览器自适应的，所以不写也可以
			ServletOutputStream os = response.getOutputStream();
			os.write(buffer);
			os.flush();
			os.close();
			break;
		}
		case "reg":{
			BBSUser registerUser = service.upload(request);
			boolean flag = service.register(registerUser);
			if(flag){
				request.setAttribute("information", "注册成功！");
				dispatcher = request.getRequestDispatcher("article?action=query&curpage=1");
			}
			break;
		}
		case "page":{
			String PAGENUM = request.getParameter("row");
			String id = request.getParameter("userid");
			boolean flag = service.chancePAGENUM(Integer.parseInt(PAGENUM), Integer.parseInt(id));
			if(flag){
				dispatcher = request.getRequestDispatcher("article?action=query&curpage=1");
			}
			break;
		}
		default:
			break;
		}
		if(dispatcher != null){
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
